<?php

namespace App\Admin\Controllers;

use App\Operator;
use App\OperatorLine;
use App\OperatorType;
use App\Providers\OperatorLinesServiceProvider;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;

class JfController extends CommonController
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('机房管理');

            $grid = Admin::grid(Operator::class, function(Grid $grid){

                $grid->id('ID')->sortable();

                $grid->column('name',"机房名称");


                $grid->line('线路')->display(function ($lineId){
                    return OperatorLine::find($lineId)->name ;
                });

                $grid->type('类型')->display(function ($typeId){
                    return OperatorType::find($typeId)->name ;
                });


                $grid->status('状态')->display(function ($status) {
                    return $status ? '开启' : '关闭';
                });

                $grid->filter(function ($filter) {
                    $filter->where(function ($query) {
                        $query->where('name', 'like', "%{$this->input}%");
                    }, '机房名称');
                });
            });
            $content->body($grid);
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('机房编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新增机房');
            $content->description('description');
            $grid = Admin::form(Operator::class, function(Form $form){


                // 添加text类型的input框
                $form->text('name', '机房名称');

                $line = new OperatorLine();
                $getline = $line->getlines();
                $lines = $this->filter($getline);

                $form->select('line', '线路')->options($lines);

                $type = new OperatorType();
                $gettype = $type->gettypes();
                $types = $this->filter($gettype);

                $form->select('type', '机房类型')->options($types);

                $states = [
                    'on'  => ['value' => 1, 'text' => '打开', 'color' => 'success'],
                    'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
                ];
                $form->switch('status', "状态")->states($states);

                // 两个时间显示
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '修改时间');
            });
            $content->body($grid);
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Operator::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Operator::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name',"机房名称");
            $line = new OperatorLine();
            $getline = $line->getlines();
            $lines = $this->filter($getline);

            $form->select('line', '线路')->options($lines);

            $type = new OperatorType();
            $gettype = $type->gettypes();
            $types = $this->filter($gettype);

            $form->select('type', '机房类型')->options($types);

            $states = [
                'on'  => ['value' => '1', 'text' => '打开', 'color' => 'success'],
                'off' => ['value' => '0', 'text' => '关闭', 'color' => 'danger'],
            ];
            $form->switch('status', "状态")->states($states);

        });
    }

    public function store(Request $request)
    {
        $jf = new Operator();
        $jf->name = $request->input('name');
        $jf->line = $request->input('line');
        $jf->type = $request->input('type');
        $jf->status = ($request->input('status') != "on")?0:1;
        $jf->save();
    }


}
